package com.customization.shunsheng;

import com.api.browser.bean.SearchConditionItem;
import com.customization.shunsheng.util.LeaveTypeBrowseButtonUtil;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.kq.cmd.leavetypes.GetLeaveTypesFormCmd;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.List;
import java.util.Map;

import static com.api.browser.util.ConditionType.CHECKBOX;

@CommandDynamicProxy(target = GetLeaveTypesFormCmd.class )
public class GetLeaveTypesFormCmdIntercept extends AbstractCommandProxy<Map<String,Object>> {
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {
        GetLeaveTypesFormCmd cmd = (GetLeaveTypesFormCmd) command;
        Map<String,Object> params=cmd.getParams();
        Map<String,Object> result=nextExecute(cmd);

        RecordSet recordSet=new RecordSet();
        User user = cmd.getUser();
        String viewtype="";
        String typeId = Util.null2String(params.get("typeId"));
        if (StringUtil.isNotNull(typeId)){
            viewtype="edit";
        }else {
            viewtype="add";
        }

        result = LeaveTypeBrowseButtonUtil.handleTaskTempletFormCmd(viewtype, params, result);

        List<Object> condition1 = (List<Object>) result.get("condition");
        Map<String,Object> items = (Map<String, Object>) condition1.get(0);
        List<SearchConditionItem> item = (List<SearchConditionItem>) items.get("items");

        if ("edit".equals(viewtype)) {
            //增加一个是否外籍的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"foreignLeave"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("是否外籍");
            //获取是否外籍的value
            Integer isForg = -1;
            String getForSql = "select isForg from kq_LeaveRules where id = ?";
            recordSet.executeQuery(getForSql,typeId);
            while (recordSet.next()) {
                isForg = Util.getIntValue(Util.null2String(recordSet.getString("isForg")));
            }
            if (isForg == -1 || isForg == 0) {
                scConItem.setValue("0");
            } else {
                scConItem.setValue("1");
            }
            scConItem.setHelpfulTip("勾选外籍后，请在员工选择栏选择需要添加的外籍员工，并选择需要对应的非外籍的假期类型");
            item.add(scConItem);
        } else {
            //增加一个是否外籍的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"foreignLeave"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("是否外籍");
            scConItem.setValue("0");
            scConItem.setHelpfulTip("勾选外籍后，请在员工选择栏选择需要添加的外籍员工，并选择需要对应的非外籍的假期类型");
            item.add(scConItem);
        }

        return result;
    }
}
